import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:mtf/R.dart';
import 'package:mtf/app/data/RechargeRecordEntity.dart';
import 'package:mtf/app/modules/recharge_record_module/recharge_record_controller.dart';
import 'package:mtf/app/theme/app_boxDecoration.dart';
import 'package:mtf/app/theme/app_colors.dart';
import 'package:mtf/app/utils/AppUtils.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
/**
 * GetX Template Generator - fb.com/htngu.99
 * */

class rechargeRecordPage extends GetView<rechargeRecordController> {
  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
      length: 4,
      child: Builder(
          builder: (context) {
            //   final tabController = DefaultTabController.of(context)!;
            //   tabController.addListener(() {
            //     print('tabController${tabController.index}');
            // });
            return Scaffold(
              appBar: AppBar(
                elevation: 0,
                centerTitle: true,
                leading: IconButton(
                    icon: Icon(
                      Icons.arrow_back_ios,
                      color: Colors.white,
                      size: 22,
                    ),
                    onPressed: () => Navigator.pop(context)),
                title: Text("充值记录"),
                bottom: PreferredSize(
                  preferredSize: Size.fromHeight(45),
                  child: Material(
                    color: Colors.white,
                    child: TabBar(
                      indicatorColor: c_f12f38,
                      // 标签栏中选中的线（指示器）的颜色
                      indicatorSize: TabBarIndicatorSize.label,
                      // 线的长度
                      indicatorWeight: 3.0,
                      // 线的宽度
                      labelColor: c_333333,
                      unselectedLabelColor: c_666666,
                      tabs: [
                        Tab(text: "全部"),
                        Tab(text: "审核中"),
                        Tab(text: "已通过"),
                        Tab(text: "已驳回"),
                      ],
                    ),
                  ),
                ),
              ),
              body: TabBarView(
                children: RechargePages(),
              ),
            );
          }
      ),
    );
  }

  List<Widget> RechargePages() {
    var state = [-1, 0, 1, 2];
    List<Widget> pages = [];
    for (int i = 0; i < 4; i++) {
      RefreshController refreshController = RefreshController(
          initialRefresh: true);
      pages.add(RefreshConfiguration(
        headerBuilder: () => ClassicHeader(),
        footerBuilder: () => ClassicFooter(),
        child: SmartRefresher(
          controller: refreshController,
          enablePullDown: true,
          enablePullUp: true,
          onRefresh: () {
            controller.refreshData(refreshController, state[i]);
          },
          onLoading: () {
            controller.loadData(refreshController, state[i]);
          },
          child: Container(
            margin: EdgeInsets.all(10),
            decoration: r10WhiteDecoration,
            child: Obx(() {
              return  controller.getList(state[i]).length<1? Align(
                alignment: Alignment.center,
                child: Visibility(visible: controller.getList(state[i]).length<1,child: Column(
                  children: [Image.asset(R.assetsImgBgEmpty,height: 150,width: 150,),
                    Text("暂无记录")],
                )),
              ):
                ListView.separated(
                  physics: NeverScrollableScrollPhysics(),
                  separatorBuilder: (context, index) {
                    return Container(
                        margin: EdgeInsets.symmetric(horizontal: 15),
                        child: Divider(height: 1));
                  },
                  itemBuilder: (context, index) {
                    return recordItem(controller.getList(state[i])[index],i);
                  },
                  itemCount: controller.getList(state[i]).length,
                  shrinkWrap: true);
            }),
          ),
        ),
      ));
    }
    return pages;
  }

  Widget recordItem(ListRechargeElement bean,int index) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 10, vertical: 15),
      width: double.infinity,
      child: Column(
        children: [
          Flex(
            direction: Axis.horizontal,
            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(
                "充值金额",
                style: TextStyle(color: c_333333, fontSize: 15),
              ),
              Text(
                "￥"+bean.amount.toString() ??"",
                style: TextStyle(color: c_333333, fontSize: 15),
              ),
            ],
          ),
          SizedBox(
            height: 5,
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(
                "预计到账${bean.rateAmount}USD",
                style: TextStyle(color: c_333333, fontSize: 12),
              ),
              Text(
                bean.getStatusValue(),
                style: TextStyle(color: c_f12f38, fontSize: 12),
              )
            ],
          ),
          Container(
            width: double.infinity,
            margin: EdgeInsets.symmetric(vertical: 10),
            child: GridView.builder(
              itemCount: bean.rechargeImg!.length,
              shrinkWrap: true,
              physics: NeverScrollableScrollPhysics(),
              gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
                crossAxisSpacing: 10, //水平子 Widget 之间间距
                crossAxisCount: 6, //一行的 Widget 数量
              ),
              itemBuilder: (context, index) {
                return Image.network(
                  bean.rechargeImg![index] ??"https://via.placeholder.com/56x56",
                  fit: BoxFit.fill,
                  height: 56,
                  width: 56,
                );
              },
            ),
          ),
          Align(alignment: Alignment.centerLeft,
              child: Text(
                AppUtils.dateToString(bean.updatedAt) ?? "", style: TextStyle(color: c_999999),)),
          Visibility(
            visible: index == 3,
            child: Container(
              margin: EdgeInsets.only(top: 10),
              padding: EdgeInsets.all(10),
              width: double.infinity,
              decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(4), color: c_f4f4f4),
              child: Text.rich(TextSpan(children: [
                TextSpan(text: "驳回原因：",
                    style: TextStyle(color: Color(0xff407FDC), fontSize: 12)),
                TextSpan(text: bean.checkNote,
                    style: TextStyle(color: c_666666, fontSize: 12))
              ])),
            ),
          )
        ],
      ),
    );
  }
}